package com.example.origin.finance.trade.service;

import cn.hutool.core.date.DateUtil;
import com.example.origin.finance.dto.XueqiuKline;
import com.example.origin.finance.enums.TableNameEnum;
import com.example.origin.finance.utils.TableUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

public abstract class AbstractModelService {
    @Resource
    private MongoTemplate mongoTemplate;



    public   <T> List<T> find(Query query, TableNameEnum tableNameEnum) {
        return TableUtil.find(mongoTemplate,query,tableNameEnum);
    }

    protected static void filterKline(List<XueqiuKline> xueqiuKlines) {
        List<XueqiuKline> list = new ArrayList<>(xueqiuKlines.size());
        Map<Date,Integer> map = new HashMap<>();
        for (XueqiuKline xueqiuKline : xueqiuKlines) {
            if (6== DateUtil.dayOfWeek(xueqiuKline.getTimestamp())) {
                list.add(xueqiuKline);
            }
            if (5== DateUtil.dayOfWeek(xueqiuKline.getTimestamp())) {
                list.add(xueqiuKline);
            }
            if (4== DateUtil.dayOfWeek(xueqiuKline.getTimestamp())) {
                list.add(xueqiuKline);
            }
            if (!map.containsKey(xueqiuKline.getTimestamp())) {
                map.put(xueqiuKline.getTimestamp(),1);
            }else {
                map.put(xueqiuKline.getTimestamp(),map.get(xueqiuKline.getTimestamp())+1);
            }
        }
        for (Map.Entry<Date, Integer> entry : map.entrySet()) {
            if (entry.getValue()<100) {
                list.removeIf(e->e.getTimestamp().equals(entry.getKey()));
            }
        }
        xueqiuKlines.clear();
        xueqiuKlines.addAll(list);
    }
}
